import { shareGoodOrder } from "@src/api/reservation";
const App = getApp();
import base from "@src/store/base";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import { SHARE_PATH } from "@src/common/reservation/constant/index.js";
import member from "@src/store/member";
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["isiPhoneX", "themeColor", "themeTitleColor"],
    },
    {
      store: member,
      fields: ["wxName", "wxHeadImg"],
    },
  ],
  /**
   * 组件的属性列表
   */
  properties: {
    shopInfo: {
      type: Object,
      value: {},
    },
    orderDetail: {
      type: Object,
      value: {},
    },
  },
  options: {
    addGlobalClass: true,
  },

  ready() {
    this.handleShare();
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgData: {},
  },

  /**
   * 组件生命周期
   */
  lifetimes: {
    attached: function (val) {},
    ready: function (val) {
      this.handleShare();
    },
    moved: function () {},
    detached: function () {},
  },

  /**
   * 组件所在页面生命周期
   */
  pageLifetimes: {},

  /**
   * 组件的方法列表
   */
  methods: {
    async handleShare() {
      wx.showLoading({ title: "加载海报中" });
      const {
        shopInfoResp: { shopId, shopConfigId },
      } = this.data.shopInfo;
      const { goodsVOList, appointmentPlanId } = this.data.orderDetail;
      const { code, data } = await shareGoodOrder({
        goodsSerial: goodsVOList[0].goodsSerial,
        // goodsSerial: goodsVOList[0].goodsSerial,
        id: appointmentPlanId,
        planId: appointmentPlanId,
        path: SHARE_PATH,
        shopConfigId,
        shopId,
      }).catch(() => {
        wx.hideLoading();
      });
      if (code === 0) {
        wx.hideLoading();
        console.log("商品详情", data);
        this.setData({
          imgData: data,
        });
      }
    },
    //放大二维码功能
    enlargeFenc(e) {
      console.log("e", e);
      let img = e.currentTarget.dataset.img;
      let type = e.currentTarget.dataset.type;
      this.selectComponent("#enlargeImage").dialogShow(img, type);
    },
    setHiddenFunc() {
      console.log("关闭");
      this.triggerEvent("close");
    },
    noMake() {},
    handleSaveQRCode() {
      wx.downloadFile({
        url: this.data.imgData.wxQrCodeUrl,
        success: (res) => {
          if (res.statusCode === 200) {
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: () => {
                wx.showToast({
                  title: `海报已保存到系统相册`,
                  icon: "none",
                  duration: 3000,
                });
                this.setHiddenFunc();
              },
              fail: (err) => {
                console.log(err);
                if (
                  err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
                  err.errMsg === "saveImageToPhotosAlbum:fail auth deny"
                ) {
                  // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
                  wx.showModal({
                    title: "提示",
                    content: "需要您授权保存相册",
                    showCancel: false,
                    success: () => {
                      wx.openSetting({
                        success(settingdata) {
                          if (
                            settingdata.authSetting["scope.writePhotosAlbum"]
                          ) {
                            this.handleSaveQRCode();
                          } else {
                            wx.showModal({
                              title: "提示",
                              content:
                                "获取保存到相册权限失败，将无法保存到相册",
                              showCancel: false,
                            });
                          }
                        },
                      });
                    },
                  });
                }
              },
            });
          }
          console.log(res);
        },
      });
    },
  },
});
